package com.ad.pet.app.account.record;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ad.pet.DaggerMainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.account.adapter.WithDawerCountAdapter;
import com.ad.pet.app.account.share.ShareDialog;
import com.ad.pet.app.recharge.CashMoneyContracts;
import com.ad.pet.app.recharge.CashMoneyPresenter;
import com.ad.pet.app.shopcar.confirm.ConfirmOrderContracts;
import com.ad.pet.app.shopcar.confirm.ConfirmOrderPresenter;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.BaseActivity;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.account.Banlance;
import com.ad.pet.models.remote.account.CountDrawTotal;
import com.ad.pet.models.remote.account.UserBuyReturn;
import com.ad.pet.models.remote.account.order.PayCompleted;
import com.ad.pet.models.remote.account.order.SignCheck;
import com.ad.pet.models.remote.cart.BigShipingw;
import com.ad.pet.models.remote.cart.Coupons;
import com.ad.pet.models.remote.mall.Counpous;
import com.ad.pet.models.remote.recharge.WithDraw;
import com.ad.pet.ui.AutoPollRecyclerView;
import com.ad.pet.ui.InvatieFriendsHelpMeOpenDialog;
import com.ad.pet.ui.OpenRedBagDialog;
import com.ad.pet.ui.SecondOpenRedbagDialog;
import com.ad.pet.utils.LyxUtils;
import com.ad.pet.utils.StatusBarUtil;
import com.ad.pet.utils.UMEventUtil;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

public class AccountRecordActivity extends BaseActivity implements CashMoneyContracts.View, ConfirmOrderContracts.View {
    @BindView(R.id.card_tixian)
    CardView mCardTixian;

    @BindView(R.id.tv_80p)
    TextView mTv80p;


    @BindView(R.id.tv_today_count)
    TextView mTvTodayCount;
    @BindView(R.id.tv_today_has_cash)
    TextView mTvTodayHasCash;

    @BindView(R.id.recycler_erver)
    AutoPollRecyclerView mRecyclerErver;

    @BindView(R.id.tv_cash_now_money)
    TextView mTvCashNowMoney;

    @BindView(R.id.releate_gonglve)
    RelativeLayout mRelativeLayoutGongLve;

    @BindView(R.id.iv_back)
    ImageView mIvBack;

    @BindView(R.id.tv_all_banlance)
    TextView mTvAllBanlance;

    private WithDawerCountAdapter mWithDawerCountAdapter;
    @Inject
    CashMoneyPresenter mCashMoneyPresenter;

    @BindView(R.id.tv_rullor)
    TextView mTvRullor;
    @Inject
    RxDisposable mRxDisposable;
    @Inject
    ConfirmOrderPresenter mConfirmOrderPresenter;

    @BindView(R.id.relatice_share_recode)
    RelativeLayout mRelativeShareRecord;

    @BindView(R.id.tv_yue_value)
    TextView mTvYueValue;

    @BindView(R.id.tv_cash_redbag)
    TextView mTvRedCash;
    @BindView(R.id.btn_cash)
    TextView mBtnCash;
    @BindView(R.id.btn_day)
    TextView mBtnDay;

    private SignCheck mSignCheck;
    private SignCheck mSignCode;


    private SignCheck mDaySign;
    private ShareDialog mShareDialog;
    private SecondOpenRedbagDialog mSecondOpenRedbagDialog;

    private Button mBtnOpenInvateOpen;
    InvatieFriendsHelpMeOpenDialog mHelpMeOpenDialog;
    OpenRedBagDialog mOpenRedBagDialog;
    private TextView mTvTips;
    private TextView mTvCongration;
    private TextView mTvRedValue;
    //    private TextView mTvCanCash;
    private TextView mTv_unopen;
    private Button mBtnHelp;


    @Override
    protected void initViews() {
        mConfirmOrderPresenter.onCreate(this);
        mConfirmOrderPresenter.get_user_balance();
        mConfirmOrderPresenter.shareH5Url();

        mShareDialog = new ShareDialog(mActivity);
        mHelpMeOpenDialog = new InvatieFriendsHelpMeOpenDialog(mActivity, R.style.custom_dialog_style);
        mBtnOpenInvateOpen = mHelpMeOpenDialog.findViewById(R.id.btn_invate_open);
        mTvTips = mHelpMeOpenDialog.getTipsView();


        mOpenRedBagDialog = new OpenRedBagDialog(mActivity, R.style.custom_dialog_style);
        mTvCongration = mOpenRedBagDialog.getView().findViewById(R.id.tv_cash);
        mTvRedValue = mOpenRedBagDialog.getView().findViewById(R.id.tv_value);
//        mTvCanCash = mOpenRedBagDialog.getView().findViewById(R.id.tv_cancash);
        mTv_unopen = mOpenRedBagDialog.getView().findViewById(R.id.tv_unopen);
        mBtnHelp = mOpenRedBagDialog.getView().findViewById(R.id.btn_help);

        mSecondOpenRedbagDialog = new SecondOpenRedbagDialog(mActivity, R.style.custom_dialog_style);

        mSecondOpenRedbagDialog.setCancelable(false);
    }

    @Override
    protected void initData() {
        mRecyclerErver.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mWithDawerCountAdapter = new WithDawerCountAdapter(null);
        mRecyclerErver.setAdapter(mWithDawerCountAdapter);
        mRecyclerErver.start();

        mCashMoneyPresenter.onCreate(this);
        mCashMoneyPresenter.getCurrentTotalWithdrawal();
    }

    @Override
    protected void initEvents() {
        mTvRullor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCommonWebView(mActivity, mTvRullor.getText().toString(), getString(R.string.ip_default) + "/static/redpackage_use.html?lang=" + SpUtil.getInstance().getString(Constant.LANGUAGE, "cn"));
            }
        });

        mTvCashNowMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCashMoney(mActivity);
            }
        });

        mRelativeLayoutGongLve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigation.goCommonWebView(mActivity, "", "/event/savingGuide");
            }
        });

        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mBtnCash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UMEventUtil.onEvent(mActivity, "share_friend_event");
                if (mSignCode != null) {
                    initUMshare();
                }
            }
        });
        mBtnDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                UMEventUtil.onEvent(mActivity, "share_day_event");
                mConfirmOrderPresenter.checkIsSign();

            }
        });

        mRelativeShareRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Navigation.goShareRecord(mActivity);
            }
        });
        mBtnHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });

        mHelpMeOpenDialog.findViewById(R.id.relativeLayout_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSignCheck != null) {
                    if (mSignCheck.getStatus() == 1) {
                        mConfirmOrderPresenter.daySign();
                    }
                }
            }
        });


        mBtnOpenInvateOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHelpMeOpenDialog.dismiss();
                initUMshare();
            }
        });
    }

    private void initUMshare() {
        if (mSignCode == null) {
            return;
        }
//        UMWeb mUMWeb = new UMWeb(mSignCode.getUrl());
//        UMImage mUmImage = new UMImage(getActivity(), R.drawable.ic_share_img);
//        mUMWeb.setThumb(mUmImage);
//        mUMWeb.setTitle(getString(R.string.share_title));
//        mUMWeb.setDescription(getString(R.string.share_desc));
//        new ShareAction(getActivity())
//                .withMedia(mUMWeb)
//                .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.FACEBOOK)
//                .setCallback(new UMShareLinstener()).open(mBoardConfig);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_share_img);

        mShareDialog.share(mSignCode.getUrl(), getString(R.string.share_title), bitmap, getString(R.string.share_desc));
        mShareDialog.show();
    }

    @Override
    public void initInject() {
        DaggerMainActivityCompent.builder().applicationComponent(getApplicationComponent())

                .activityModule(getActivityModule()).build().inject(this);
    }

    @Override
    protected void setContentViewBefore() {
        super.setContentViewBefore();
        StatusBarUtil.setTranslucentStatus(this);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_record_layout_account;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void getWithDrawSuccess(WithDraw w) {

    }

    @Override
    public void addWithDrawSuccess() {

    }

    @Override
    public void getCurrentTotalWithdrawalSuccess(CountDrawTotal countDrawTotal) {


        mTv80p.setText(countDrawTotal.getLang().getReferring_notice());
        mTvTodayCount.setText(countDrawTotal.getLang().getCurrent_total_withdrawal());
        mTvTodayHasCash.setText(countDrawTotal.getLang().getCurrent_now_data());

        List<String> strings = new ArrayList<>();
        for (int i = 0; i < countDrawTotal.getRoll_text().getText().size(); i++) {
            strings.add(countDrawTotal.getRoll_text().getTime().get(i) + "      " + countDrawTotal.getRoll_text().getText().get(i));
        }
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);
        strings.addAll(strings);


        mWithDawerCountAdapter.setNewData(strings);
    }

    @Override
    public void error(int code, String msg) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }




    @Override
    public void getCouponsSuccess(List<Counpous> coupons) {

    }

    @Override
    public void checkCouponsSuccess(Coupons coupons) {

    }

    @Override
    public void confirmOrderSuccess(PayCompleted shop) {

    }

    @Override
    public void getUserBanceSuccess(Banlance banlance) {
        mTvAllBanlance.setText("" + LyxUtils.formatAdd(Double.valueOf(banlance.getBalance()) + Double.valueOf(banlance.getBalance_red_envelope()), Double.valueOf(banlance.getRed_envelope())));
        mTvYueValue.setText("US $" + LyxUtils.formatAdd(Double.valueOf(banlance.getBalance()), Double.valueOf(banlance.getBalance_red_envelope())));

        mTvRedCash.setText("US $" + banlance.getRed_envelope());
    }

    @Override
    public void getShipingWaysSuccess(BigShipingw shippingWay) {

    }

    @Override
    public void paymentBalanceSuccess(int toPay) {

    }

    @Override
    public void checkIsSignSuccess(SignCheck signCheck) {
        mSignCheck = signCheck;

        if (signCheck.getStatus() == 1 && signCheck.getFirst_mark() == 1) {


            mSecondOpenRedbagDialog.show();


            mRxDisposable.add(Observable.interval(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
                    .take(13)
                    .subscribe(new Action1<Long>() {
                        @Override
                        public void call(Long aLong) {

                            String format = String.format("%ds", 13 - aLong);
                            RxTextView.text((TextView) mSecondOpenRedbagDialog.findViewById(R.id.tv_time)).call(format);


                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {

                        }
                    }, new Action0() {
                        @Override
                        public void call() {
                            mConfirmOrderPresenter.daySign();
                        }
                    }));


        } else if (signCheck.getStatus() == 0) {
            mTvTips.setText(getString(R.string.chance_today_has_been_over));
            mBtnOpenInvateOpen.setVisibility(View.VISIBLE);
            mHelpMeOpenDialog.show();
        } else if (signCheck.getStatus() == 1) {
            mHelpMeOpenDialog.show();
        }
    }

    @Override
    public void daySignSuccess(SignCheck signCheck) {
        mDaySign = signCheck;

        mHelpMeOpenDialog.dismiss();

        if (mDaySign.getAction_button() == 2 || mDaySign.getAction_button() == 3) {
            mSecondOpenRedbagDialog.dismiss();
            mRxDisposable.clear();
        }

        mTvCongration.setText(signCheck.getStr_head());
        mTvRedValue.setText(SpUtil.getInstance().getString(Constant.CURRENCY_LABEL, "US $") + signCheck.getPrice());
        mTv_unopen.setText(signCheck.getStr_foot());
        mBtnHelp.setText(signCheck.getStr_button());
        mConfirmOrderPresenter.get_user_balance();
        mOpenRedBagDialog.show();
    }

    @Override
    public void sharH5UrlSuccess(SignCheck signCheck) {
        mSignCode = signCheck;
    }

    @Override
    public void get_user_buy_return_success(UserBuyReturn userBuyReturn) {

    }
}
